<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\ComCoupon;
use app\api\model\IntegralGainRecord;
use app\api\model\PreferentialSettings;
use app\api\model\User;
use think\facade\Db;
use think\Request;
use app\api\model\CouponExchange as CouponExchangeModel;
class CouponExchange
{
    /**
     * php think make:model api@CouponExchange
     *积分兑换商品展示
     * @return \think\Response
     */
    public function index()
    {
        //查询展示商品
        $data=CouponExchangeModel::showGoods();
        if ($data){
            return json(['code'=>10200,'msg'=>'查询成功','data'=>$data]);
        }else{
            return json(['code'=>10501,'msg'=>'查询失败']);
           // preferential_settings

        }

    }

    /**
     * @param Request $request
     * @return \think\response\Json|void
     * 积分购买
     */
    public function integralToBuy(Request $request){
        $id=input();//要兑换的商城物品id
       $uid= $request->uid;//获取用户id===jwt
        //模拟数据
        $uid=1;
        $id=1;
       if (!$uid){
           return json(['code'=>10502,'msg'=>'未登录','data'=>'']);
       }
       //查询用户信息,
        $userdata=User::userDetails($uid);
        $userdata=$userdata->toArray();
        //查询要兑换的物品信息,
        $shopdata=CouponExchangeModel::shopDetails($id);
        $shopdata=$shopdata->toArray();
        if ($userdata['points']<0){
            return json(['code'=>10503,'msg'=>'积分为负,不能兑换','data'=>'']);
        }
       $minus= $userdata['points']-$shopdata['price'];
        if ($minus<0){
            return json(['code'=>10503,'msg'=>'积分不足,请重新选择','data'=>'']);
        }



        try {
            if($shopdata['type']=='0'){
                //查询要兑换的优惠卷数据
                $getOneShowData=PreferentialSettings::getOneShow($shopdata['coupon_id']);
                $getOneShowData=$getOneShowData->toArray();

                //为要添加的数据做拼装
                $where['user_id']=$uid;
                $where['name']=$getOneShowData['name'];
                $where['condition']=$getOneShowData['acme'];
                $where['type']=$getOneShowData['uset_threshold'];
                $where['discount']=$getOneShowData['discount'];
                $where['count']=0;
                $where['starttime']= date("Y-m-d H:i:s",time()) ;
                $where['endtime']= date("Y-m-d H:i:s",time()+86400*30)     ;
                //给用户添加优惠卷
                $userAddCoupons=ComCoupon::userAddCoupons($where);
                if ($userAddCoupons==1){
                    $data =['code'=>10203,'msg'=>'积分兑换优惠卷成功'];
                }
                //修改用户积分减去
                $updaUser=  Db::table('ea_users')->update(['id' => $uid,'points' => $minus]);

            }

            //添加用户积分记录表
           // integral.GainRecord/index
            $Addingentries['users_id']=$uid;
            $Addingentries['score']=$minus;
            $Addingentries['type']=2;
            $Addingentries['create_time']=time();
            IntegralGainRecord::Addingentries($Addingentries);

            return json($data);

        } catch (\Exception $e){

        }












    }


}
